package st.business.tepth.business.user.operationchoose;

import com.alibaba.fastjson.TypeReference;

import java.util.List;

import io.reactivex.disposables.Disposable;
import st.business.tepth.base.OnDataConvertListener;
import st.business.tepth.business.bean.OperationBean;
import st.business.tepth.config.Url;
import st.shadow.library.core.net.callback.IError;
import st.shadow.library.core.net.callback.ISuccess;
import st.shadow.library.core.net.data.ObjConverter;
import st.shadow.library.core.net.rx.RxRestClient;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-03-19 09:40
 */

public class OperationChooseModel implements OperationChooseContract.Model {

    @Override
    public Disposable getOperationList(String token,
                                       final OnDataConvertListener<List<OperationBean>> listener) {
        return RxRestClient.builder()
                .url(Url.GetActionList)
                .params("token", token)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        listener.convert(convertOperationList(response));
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        listener.error(code, msg);
                    }
                })
                .build()
                .post();
    }

    private List<OperationBean> convertOperationList(String response) {
        return new ObjConverter<List<OperationBean>>().convert(response
                , new TypeReference<List<OperationBean>>() {
                });
    }
}
